﻿using System;
using System.Collections;
using System.Data;

public partial class Admin_LinhVuc_LinhVucConChiTiet : System.Web.UI.Page
{
    Data data = new Data();
    int indexOfImg = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["admin"].Equals("true") == false)
            {
                Response.Redirect("../Default.aspx");
            }
            else
            {
                if (IsPostBack == false)
                {
                    FillData();
                }
                else
                {
                    Panel1.Visible = false;
                }
                if (Request.QueryString["delete"] != null)
                {
                    string tableNameParent = data.GetTable_MucLucPhapLuat_NameByID(Request.QueryString["MucPhapLuatID"]);
                    string tableNameChild = data.GetTable_MucLucPhapLuatChild_Name(Request.QueryString["TableParentID"], tableNameParent);
                    data.Delete_MucPhapLuatConChiTiet(tableNameChild, Request.QueryString["delete"]);
                }
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("../Default.aspx");
            throw;
        }
    }

    private void FillData()
    {
        Calendar1.SelectedDate = DateTime.Now;
        DataTable table = data.GetTable_By_Name("MucLucPhapLuat");
        comboboxCha.DataSource = table;
        comboboxCha.DataTextField = "MucPhapLuatDescription";
        comboboxCha.DataValueField = "MucPhapLuatName";
        comboboxCha.DataBind();
        Panel1.Visible = false;
    }
 
    protected void btnSave_Click(object sender, EventArgs e)
    {
        String fileanh = "";
        Random random = new Random();
        String noidung = CKEditor1.Text;

        if (FileUpload1.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = FileUpload1.FileName.Split('.')[0] + "_" + _random + "." + FileUpload1.FileName.Split('.')[1] + ";";
            FileUpload1.SaveAs(Server.MapPath("~/Images/MucLucPL/" + FileUpload1.FileName.Split('.')[0] + "_" + _random + "." + FileUpload1.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload1.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        if (FileUpload2.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = fileanh + FileUpload2.FileName.Split('.')[0] + "_" + _random + "." + FileUpload2.FileName.Split('.')[1];
            FileUpload2.SaveAs(Server.MapPath("~/Images/MucLucPL/" + FileUpload2.FileName.Split('.')[0] + "_" + _random + "." + FileUpload2.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload2.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        if (FileUpload3.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = fileanh + FileUpload3.FileName.Split('.')[0] + "_" + _random + "." + FileUpload3.FileName.Split('.')[1] + ";";
            FileUpload3.SaveAs(Server.MapPath("~/Images/MucLucPL/" + FileUpload3.FileName.Split('.')[0] + "_" + _random + "." + FileUpload3.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload3.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        if (FileUpload4.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = fileanh + FileUpload4.FileName.Split('.')[0] + "_" + _random + "." + FileUpload4.FileName.Split('.')[1] + ";";
            FileUpload4.SaveAs(Server.MapPath("~/Images/MucLucPL/" + FileUpload4.FileName.Split('.')[0] + "_" + _random + "." + FileUpload4.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload4.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        if (FileUpload5.HasFile)
        {
            int _random = random.Next(100000);
            fileanh = fileanh + FileUpload5.FileName.Split('.')[0] + "_" + _random + "." + FileUpload5.FileName.Split('.')[1] + ";";
            FileUpload5.SaveAs(Server.MapPath("~/Images/MucLucPL/" + FileUpload5.FileName.Split('.')[0] + "_" + _random + "." + FileUpload5.FileName.Split('.')[1]));
            if (chk_source.Checked == false)
            {
                noidung = ReplaceImgSrc(noidung, FileUpload5.FileName.Split('.')[0] + "_" + _random, indexOfImg);
            }
        }
        string _MucPhapLuatID = data.GetTable_MucLucPhapLuat_ID_byName(Session["TableParentname"].ToString());
        string _DoanhNghiepID = data.GetTable_MucLucPhapLuatID_ByName(Session["TableParentname"].ToString(), Session["Tablename"].ToString());
        data.Add_MucPhapLuatConChiTiet(Session["TableParentname"] + "ID", Session["Tablename"].ToString(), _MucPhapLuatID, _DoanhNghiepID, txtTieude.Text,noidung, Calendar1.SelectedDate.ToShortDateString(), fileanh);
        Response.Redirect("Default.aspx");
    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Session["Tablename"] = null;
        Session["TableParentname"] = null;
        Response.Redirect("Default.aspx");
    }
    protected void btnChonCha_Click(object sender, EventArgs e)
    {
        DataTable table = data.GetTable_By_Name(comboboxCha.SelectedValue);
        comboboxCon.DataSource = table;
        comboboxCon.DataTextField = comboboxCha.SelectedValue + "Description";
        comboboxCon.DataValueField = comboboxCha.SelectedValue + "Name";
        comboboxCon.DataBind();
        Session["TableParentname"] = comboboxCha.SelectedValue;
    }
    protected void btnchonCon_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        DataTable table = data.GetTable_By_Name(comboboxCon.SelectedValue);
        table.Columns[1].ColumnName = "TableParentID";
        DataListYahoo.DataSource = table;
        DataListYahoo.DataBind();
        Session["Tablename"] = comboboxCon.SelectedValue;
    }
    protected String ReplaceImgSrc(String noidung, String noidungInsert, int _indexOfImg)
    {
        ArrayList imgSrc = new ArrayList();
        int indexOfSrc = noidung.IndexOf("src=", _indexOfImg);
        if (indexOfSrc != -1)
        {
            int indexOfGachcheo = noidung.IndexOf("/>", indexOfSrc);
            int indexofDot = noidung.IndexOf(".", indexOfSrc);
            if (indexofDot < indexOfGachcheo && indexOfGachcheo != -1 && indexofDot != -1)
            {
                int khoangcach = indexofDot - indexOfSrc - 5;
                noidung = noidung.Remove(indexOfSrc + 5, khoangcach);
                noidung = noidung.Insert(indexOfSrc + 5, "../Images/MucLucPL/" + noidungInsert);
            }
            indexOfImg = indexOfGachcheo;
        }

        return noidung;
    }
}
